package com.leetcode.p0;

/**
 * 时间复杂度为 n，比另一种方法更好
 */
public class Test45_2 {

    public int jump(int[] nums) {
        int steps = 0;
        int max = 0;
        int end = 0;

        for (int i = 0; i < nums.length - 1; i++) {
            max = Math.max(max, nums[i] + i);
            if (i == end) {
                end = max;
                steps++;
            }
        }
        return steps;
    }

    public static void main(String[] args) {
        Test45_2 test = new Test45_2();
//        int result = test.jump(new int[]{7, 0, 30, 6, 9, 6, 1, 7, 9, 0, 1, 2, 9, 0, 3, 1});
        int result = test.jump(new int[]{2, 3, 1, 1, 4});
        System.out.println(result);
    }

}
